package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.engine4cloud.tdf.admin.api.feign.RemoteDeptService;
import com.engine4cloud.tdf.common.core.constant.SecurityConstants;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.core.util.RetOps;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.dto.query.GzryStaffQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.GzryStaffResultDTO;
import com.engine4cloud.tdf.supervision.mapper.GzryStaffMapper;
import com.engine4cloud.tdf.supervision.service.GzryStaffService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GzryStaffServiceImpl implements GzryStaffService {

    @Resource
    private GzryStaffMapper gzryStaffMapper;
    @Resource
    private RemoteDeptService remoteDeptService;

    @Override
    public PageInfo<GzryStaffResultDTO> getPageList(Params pageable, GzryStaffQueryDTO dto) {
        TdfUser user = SecurityUtils.getUser();
        R<List<Long>> deptIdRemoteList = remoteDeptService.getDescendantIdList(user.getDeptId(), SecurityConstants.FROM_IN);

        List<Long> deptIdList = RetOps.of(deptIdRemoteList).getData().filter(CollectionUtils::isNotEmpty)
                .orElseThrow(() -> new ValidateCodeException("没有数据权限!"));

        IPage<GzryStaffResultDTO> page = gzryStaffMapper.pageList(Pages.page(pageable), dto, deptIdList);
        return Pages.convert(page);
    }
}
